﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using Common;
using System.Data.Linq;
namespace BAL
{
    public class Subject
    {
        private static string CacheName = "AllSubject";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.Subjects.ToList(), 0, 60);
            }
        }
        public static IDictionary<int, string> GetDic()
        {
            if (!CacheHelper.IsExist(CacheName))
            {
                ToCache();
            }
            IList<Models.Subject> cache = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            IDictionary<int, string> dictionary = new Dictionary<int, string>();
            foreach (Models.Subject subject in cache)
            {
                dictionary.Add(new KeyValuePair<int, string>(subject.Id, subject.Name));
            }
            return dictionary;
        }

 

        internal static SubjectInfo TransSubject2SubjectInfo(Models.Subject subject)
        {
            SubjectInfo subjectInfo = new SubjectInfo();

            Utility.SetObjectValueWithTheSamePropertyName(subjectInfo, subject);

            return subjectInfo;
        }

        public static SubjectInfo AddSubject(SubjectInfo subjectInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Subject subject = new Models.Subject();
                Utility.SetObjectValueWithTheSamePropertyName(subject, subjectInfo);
                DatabaseHelper.Insert<Models.Subject>(subject);
                ToCache();
                return TransSubject2SubjectInfo(subject);
            }
        }

        public static void EditSubject(SubjectInfo subjectInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.Subject subject = db.Subjects.Single(b => b.Id == subjectInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(subject, subjectInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
        public void Update(int id, string name)
        {

        }
        public void Delete(int id)
        {

        }
        public static void DelSubject(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelSubject(new int[] { id });
            }
        }

        public static void DelSubject(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    foreach (int id in lstId)
                    {
                        Book.DelBookBySubject(id);
                    }
                    string sql = string.Format("delete from Subject where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
                    ToCache();

                }
            }
        }

        public static int GetSubjectIdByKeyword(string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return Subject_Info.Single(t => t.Keyword == keyword).Id;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Subjects.Single(t => t.Keyword == keyword).Id;
            //}
        }

        public static SubjectInfo GetSubjectInfoById(int Id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return TransSubject2SubjectInfo(Subject_Info.Single(b => b.Id == Id));
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    Models.Subject subject = db.Subjects.Single(b => b.Id == Id);
            //    return TransSubject2SubjectInfo(subject);
            //}
        }

        public static IList<SubjectInfo> GetSubjectInfos()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return Subject_Info.Select(c => TransSubject2SubjectInfo(c)).ToList();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Subject>().Select(c => TransSubject2SubjectInfo(c)).ToList();
            //}
        }
        public static IList<SubjectInfo> GetSubjectInfoszhuangjiang(int subjectId)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return Subject_Info.Where(c=>c.Id==subjectId).Select(c => TransSubject2SubjectInfo(c)).ToList();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Subject>().Select(c => TransSubject2SubjectInfo(c)).ToList();
            //}
        }
        public static IList<SubjectInfo> GetSubjectInfos(int first, int max)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            if (max == 0)
            {
                return Subject_Info.Select(c => TransSubject2SubjectInfo(c)).ToList();
            }
            else
            {
                return Subject_Info.Skip(first).Take(max).Select(c => TransSubject2SubjectInfo(c)).ToList();

            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Subject>().Skip(first).Take(max).Select(c => TransSubject2SubjectInfo(c)).ToList();
            //}
        }

        public static int GetSubjectInfoCount()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return Subject_Info.Count();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Subject>().Count();
            //}
        }

        public static bool CheckSubjectExistByKeyword(string keyword, int editid)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return Subject_Info.Where(w => w.Keyword == keyword && w.Id != editid).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Subjects.Where(w => w.Keyword == keyword && w.Id != editid).Count() > 0;
            //}
        }

        public static bool CheckSubjectExistById(int Id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return Subject_Info.Where(w => w.Id == Id).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Subjects.Where(w => w.Id == Id).Count() > 0;
            //}
        }

        public static bool CheckSubjectExistByName(string name,string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Subject> Subject_Info = (IList<Models.Subject>)CacheHelper.GetCache(CacheName);
            return Subject_Info.Where(w => w.Name == name || w.Keyword == keyword).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Subjects.Where(w => w.Name == name || w.Keyword == keyword).Count() > 0;
            //}
        }
    }
}
